<script lang="ts">
import {ref} from 'vue';

export default {
  name: "nav_bar",
}

</script>

<script setup lang="ts">
import "bootstrap"
import appStore from '@/store';
import {storeToRefs} from "pinia";
import {ElMessage} from "element-plus";
import {getMessageUnreadStatus} from "@/http/m_api/message";
import LoginDialog from "@/components/LoginDialog/LoginDialog";

interface Props {
  show?: boolean
  position?: "normal" | "fixed"
}


const { clearUserInfo } = appStore.useUserInfoStore;
const { saveToken } = appStore.useTokenStore;

const {userInfo, loginStatus} = storeToRefs(appStore.useUserInfoStore)
const router = useRouter()
const props = withDefaults(defineProps<Props>(), {
  show: true,
  position: "fixed",
})

const logout = () => {
  router.push({
    path: '/'
  })
  ElMessage.success('已退出登录')
  setTimeout(() => {
    clearUserInfo();
    saveToken(''); // 清除token
  },250)
}

const unreadNum = ref(0)

const getMessageInfo = async () => {
  const token = localStorage.getItem('token');
  const userInfo = localStorage.getItem('userInfo');
  if(token && userInfo){
    const data = await getMessageUnreadStatus();
    if (data.code === 0) {
      unreadNum.value = data.data.unreadCount;
    }
  }
}

getMessageInfo()
const route = useRoute()

watch(()=>route.path,
    ()=>{
  getMessageInfo()
})

</script>

<template>
  <div>
    <nav class="navbar navbar-expand-lg custom-navbar" :class="position==='fixed'? 'fixed-top':''">
      <div class="container-fluid px-4">
        <!-- Brand Section -->
        <div class="navbar-brand-section">
          <a href="/" class="brand-link">
            <img src="@/assets/logo.png" alt="logo" class="navbar-brand-logo">
          </a>
          <a class="navbar-brand custom-brand" href="/">
            智聘通
          </a>
        </div>

        <!-- Mobile Toggle Button -->
        <button class="navbar-toggler custom-toggler" type="button" data-bs-toggle="collapse"
                data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
                aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <!-- Navigation Links -->
          <ul class="navbar-nav me-auto mb-2 mb-lg-0">
            <li class="nav-item dropdown">
            <span class="nav-link dropdown-toggle custom-nav-link" href="#" role="button" data-bs-toggle="dropdown"
                  aria-expanded="false">
              <i class="fas fa-file-alt me-2"></i>
              智能简历工厂
            </span>
              <ul class="dropdown-menu custom-dropdown">
                <li>
                  <router-link class="dropdown-item custom-dropdown-item" to="/resume">
                    <i class="fas fa-edit me-2"></i>手工制作简历
                  </router-link>
                </li>
                <li>
                  <hr class="dropdown-divider">
                </li>
                <li>
                  <router-link class="dropdown-item custom-dropdown-item" to="/userinfo">
                    <i class="fas fa-user-circle me-2"></i>基本信息采集
                  </router-link>
                </li>
                <li>
                  <hr class="dropdown-divider">
                </li>
                <li>
                  <router-link class="dropdown-item custom-dropdown-item" to="/resume_create">
                    <i class="fas fa-robot me-2"></i>AI简历生成
                  </router-link>
                </li>
                <li>
                  <hr class="dropdown-divider">
                </li>
                <li>
                  <router-link class="dropdown-item custom-dropdown-item" to="/optimize">
                    <i class="fas fa-magic me-2"></i>AI简历优化
                  </router-link>
                </li>
                <li>
                  <hr class="dropdown-divider">
                </li>
                <li>
                  <router-link class="dropdown-item custom-dropdown-item" to="/analyse">
                    <i class="fas fa-chart-line me-2"></i>简历效果验证
                  </router-link>
                </li>
              </ul>
            </li>
            <li class="nav-item">
              <router-link class="nav-link custom-nav-link" to="/interview">
                <i class="fas fa-video me-2"></i>虚拟面试战场
              </router-link>
            </li>
            <li class="nav-item">
              <router-link class="nav-link custom-nav-link" to="/diagnosis">
                <i class="fas fa-stethoscope me-2"></i>面试诊疗中心
              </router-link>
            </li>
          </ul>

          <!-- User Section -->
          <div class="d-flex align-items-center" role="search" v-if="userInfo">
            <div class="user-info-section">
              <img class="navbar-avatar" :src="userInfo.avatar_url" alt="avatar"/>
              <button class="btn btn-outline-primary custom-btn me-2" type="button"
                      @click="router.push({path:'/person'})">
                <i class="fas fa-user me-1"></i>
                <el-badge :value="unreadNum" :hidden="unreadNum===0" class="item" is-dot :offset="[5,5]">
                  个人中心
                </el-badge>

              </button>
              <button class="btn btn-danger custom-btn" type="button" @click="logout">
                <i class="fas fa-sign-out-alt me-1"></i>退出登录
              </button>
            </div>
          </div>
          <div class="d-flex" role="search" v-else>
            <button class="btn btn-primary custom-btn me-2" type="button" @click="LoginDialog('login')">
              <i class="fas fa-sign-in-alt me-1"></i>登录
            </button>
            <button class="btn btn-success custom-btn" type="button" @click="LoginDialog('register')">
              <i class="fas fa-user-plus me-1"></i>注册
            </button>
          </div>
        </div>
      </div>
    </nav>

  </div>
</template>

<style lang="css"  src="bootstrap/dist/css/bootstrap.min.css"  scoped>

</style>

<style scoped lang="css">
@import 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css';

.log-overlay {
  position: fixed;
  top: 0;
  left: 0;
  min-width: 100vw;
  min-height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
}

.custom-navbar {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%) !important;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  padding: 0.75rem 0;
}

.navbar-brand-section {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-link {
  text-decoration: none;
  transition: transform 0.3s ease;
}

.brand-link:hover {
  transform: scale(1.05);
}

.navbar-brand-logo {
  height: 45px;
  width: auto;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.navbar-brand-logo:hover {
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.custom-brand {
  font-size: 1.5rem;
  font-weight: 700;
  color: white !important;
  text-decoration: none;
  background: linear-gradient(45deg, #64b5f6, #90caf9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  transition: all 0.3s ease;
}

.custom-brand:hover {
  transform: translateY(-1px);
  text-shadow: 0 2px 10px rgba(255, 255, 255, 0.3);
}

.custom-toggler {
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  padding: 0.5rem;
  transition: all 0.3s ease;
}

.custom-toggler:hover {
  border-color: rgba(255, 255, 255, 0.6);
  background-color: rgba(255, 255, 255, 0.1);
}

.custom-toggler .navbar-toggler-icon {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%28255, 255, 255, 0.8%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
}

.custom-nav-link {
  color: rgba(255, 255, 255, 0.9) !important;
  font-weight: 500;
  padding: 0.75rem 1rem !important;
  border-radius: 8px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.custom-nav-link::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transition: left 0.5s ease;
}

.custom-nav-link:hover::before {
  left: 100%;
}

.custom-nav-link:hover {
  color: white !important;
  background-color: rgba(255, 255, 255, 0.05);
  transform: translateY(-2px);
}

.custom-nav-link.router-link-active {
  background-color: rgba(255, 255, 255, 0.1);
  color: white !important;
}

.custom-dropdown {
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  padding: 0.5rem 0;
  margin-top: 0.5rem;
}

.custom-dropdown-item {
  color: #e0e0e0 !important;
  padding: 0.75rem 1.5rem;
  transition: all 0.3s ease;
  border-radius: 8px;
  margin: 0 0.5rem;
}

.custom-dropdown-item:hover {
  background: linear-gradient(135deg, #1a1a2e, #16213e);
  color: white !important;
  transform: translateX(5px);
}

.custom-dropdown-item i {
  width: 20px;
  text-align: center;
}

.custom-btn {
  border-radius: 25px;
  padding: 0.5rem 1.25rem;
  font-weight: 500;
  transition: all 0.3s ease;
  border: 2px solid transparent;
  position: relative;
  overflow: hidden;
}

.custom-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.custom-btn:hover::before {
  left: 100%;
}

.custom-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.btn-primary.custom-btn {
  background: linear-gradient(135deg, #1565c0, #0d47a1);
  border-color: transparent;
}

.btn-success.custom-btn {
  background: linear-gradient(135deg, #2e7d32, #1b5e20);
  border-color: transparent;
}

.btn-danger.custom-btn {
  background: linear-gradient(135deg, #c62828, #b71c1c);
  border-color: transparent;
}

.btn-outline-primary.custom-btn {
  color: #64b5f6;
  border-color: #64b5f6;
  background: transparent;
}

.btn-outline-primary.custom-btn:hover {
  background: linear-gradient(135deg, #1565c0, #0d47a1);
  border-color: transparent;
  color: white;
}

.user-info-section {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.navbar-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
  cursor: pointer;
}

.navbar-avatar:hover {
  border-color: rgba(255, 255, 255, 0.6);
  transform: scale(1.1);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

/* Responsive Design */
@media (max-width: 991.98px) {
  .custom-navbar {
    padding: 0.5rem 0;
  }

  .navbar-brand-logo {
    height: 35px;
  }

  .custom-brand {
    font-size: 1.25rem;
  }

  .user-info-section {
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 1rem;
  }

  .custom-btn {
    width: 100%;
    margin-bottom: 0.5rem;
  }
}

/* Animation for dropdown */
.dropdown-menu {
  animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Hover effects for navigation items */
.nav-item {
  position: relative;
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #64b5f6, #90caf9);
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.nav-item:hover::after {
  width: 80%;
}
</style>